From 4c934544696c56523bcb7e55b30607efa23c21a3 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Wed, 8 Nov 2023 17:43:17 -0700 Subject: [PATCH] convert vcf to dynamic Format. (#1210) --- CMakeLists.txt | 1 + vcf.cc | 63 +++++++++++++--------------------------- vcf.h | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ vecs.cc | 8 ++--- 4 files changed, 104 insertions(+), 47 deletions(-) create mode 100644 vcf.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 315137eef..fcbb1dc20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -238,6 +238,7 @@ set(HEADERS text.h unicsv.h units.h + vcf.h vecs.h xcsv.h xmlgeneric.h diff --git a/vcf.cc b/vcf.cc index fd3e90844..4775e753f 100644 --- a/vcf.cc +++ b/vcf.cc @@ -18,40 +18,30 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +#include "vcf.h" + #include // for fabs #include // for abs #include // for QString -#include // for QVector #include // for CaseInsensitive #include "defs.h" -#include "gbfile.h" // for gbfprintf, gbfputs, gbfclose, gbfopen, gbfile +#include "gbfile.h" // for gbfprintf, gbfputs, gbfclose, gbfopen #include "geocache.h" // for Geocache, Geocache::UtfString -static gbfile* file_out; - -static char* vcf_encrypt = nullptr; - #define MYNAME "VCF" -static -QVector vcf_args = { - { - "encrypt", &vcf_encrypt, - "Encrypt hints using ROT13", nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr - }, -}; -static void -wr_init(const QString& fname) +void +VcfFormat::wr_init(const QString& fname) { file_out = gbfopen(fname, "w", MYNAME); } -static void -wr_deinit() +void +VcfFormat::wr_deinit() { gbfclose(file_out); } @@ -60,8 +50,8 @@ wr_deinit() * Print a possibly empty input string, replacing newlines with escaped * newlines as we go. */ -static void -vcf_print_utf(const Geocache::UtfString* s) +void +VcfFormat::vcf_print_utf(const Geocache::UtfString* s) { if (nullptr == s) { return; @@ -77,8 +67,8 @@ vcf_print_utf(const Geocache::UtfString* s) gbfputs(stripped_html, file_out); } -static void -vcf_print(const char* s) +void +VcfFormat::vcf_print(const char* s) { if (!s) { return; @@ -90,14 +80,14 @@ vcf_print(const char* s) gbfputs(cleaned, file_out); } -static void -vcf_print(const QString& s) +void +VcfFormat::vcf_print(const QString& s) { vcf_print(CSTR(s)); } -static void -vcf_disp(const Waypoint* wpt) +void +VcfFormat::vcf_disp(const Waypoint* wpt) { int lonint = abs((int) wpt->longitude); int latint = abs((int) wpt->latitude); @@ -126,23 +116,10 @@ vcf_disp(const Waypoint* wpt) gbfprintf(file_out, "\nEND:VCARD\n"); } -static void -data_write() +void VcfFormat::write() { - waypt_disp_all(vcf_disp); + auto vcf_disp_lambda = [this](const Waypoint* waypointp)->void { + vcf_disp(waypointp); + }; + waypt_disp_all(vcf_disp_lambda); } - - -ff_vecs_t vcf_vecs = { - ff_type_file, - { ff_cap_write, ff_cap_none, ff_cap_none}, - nullptr, - wr_init, - nullptr, - wr_deinit, - nullptr, - data_write, - nullptr, - &vcf_args, - NULL_POS_OPS -}; diff --git a/vcf.h b/vcf.h new file mode 100644 index 000000000..7a4a53131 --- /dev/null +++ b/vcf.h @@ -0,0 +1,79 @@ +/* + Output only format for Vcard format, VCF + + Copyright (C) 2005 Robert Lipe, robertlipe+source@gpsbabel.org + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ +#ifndef VCF_H_INCLUDED_ +#define VCF_H_INCLUDED_ + +#include // for QString +#include // for QVector + +#include "defs.h" +#include "format.h" // for Format +#include "gbfile.h" // for gbfile +#include "geocache.h" // for Geocache + + +class VcfFormat : public Format +{ +public: + using Format::Format; + + QVector* get_args() override + { + return &vcf_args; + } + + ff_type get_type() const override + { + return ff_type_file; + } + + QVector get_cap() const override + { + return {ff_cap_write, ff_cap_none, ff_cap_none}; + } + + void wr_init(const QString& fname) override; + void write() override; + void wr_deinit() override; + +private: + + /* Member Functions */ + + void vcf_print_utf(const Geocache::UtfString* s); + void vcf_print(const char* s); + void vcf_print(const QString& s); + void vcf_disp(const Waypoint* wpt); + + /* Data Members */ + + gbfile* file_out{}; + + char* vcf_encrypt = nullptr; + + QVector vcf_args = { + { + "encrypt", &vcf_encrypt, + "Encrypt hints using ROT13", nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr + }, + }; + +}; +#endif // VCF_H_INCLUDED_ diff --git a/vecs.cc b/vecs.cc index 10c5fca2f..944e863ab 100644 --- a/vecs.cc +++ b/vecs.cc @@ -68,8 +68,9 @@ #include "subrip.h" // for SubripFormat #include "text.h" // for TextFormat #include "unicsv.h" // for UnicsvFormat +#include "vcf.h" // for VcfFormat #include "xcsv.h" // for XcsvStyle, XcsvFormat -#include "googletakeout.h" // for GoogleTakeoutFormat +#include "googletakeout.h" // for GoogleTakeoutFormat extern ff_vecs_t geo_vecs; @@ -86,7 +87,6 @@ extern ff_vecs_t mtk_m241_vecs; extern ff_vecs_t mtk_m241_fvecs; #endif // MAXIMAL_ENABLED #if MAXIMAL_ENABLED -extern ff_vecs_t vcf_vecs; extern ff_vecs_t gtm_vecs; #if CSVFMTS_ENABLED extern ff_vecs_t garmin_txt_vecs; @@ -136,7 +136,6 @@ struct Vecs::Impl { LegacyFormat mtk_m241_ffmt {mtk_m241_fvecs}; #endif // MAXIMAL_ENABLED #if MAXIMAL_ENABLED - LegacyFormat vcf_fmt {vcf_vecs}; UnicsvFormat unicsv_fmt; LegacyFormat gtm_fmt {gtm_vecs}; #if CSVFMTS_ENABLED @@ -317,11 +316,12 @@ struct Vecs::Impl { #endif // MAXIMAL_ENABLED #if MAXIMAL_ENABLED { - &vcf_fmt, + nullptr, "vcard", "Vcard Output (for iPod)", "vcf", nullptr, + &fmtfactory }, { &unicsv_fmt, -- 2.30.2